package com.score;

import android.app.Service;
import android.bluetooth.BluetoothHeadset;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatCheckBox;

public class ScoreMainActivity extends AppCompatActivity implements Callback {

    private CheckBox mCb, mCb2;
    private TextView mTv;
    private RLyricView mRLyric;
    private Controller mController;
    private boolean mPause = false;
    private AudioManager mAudioManager;
    private int mPitch = 0;
    private HeadSetReceiver mReceiver;
    private boolean mIsFirst = true;
    private boolean mCanEarback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_score);
        mAudioManager = (AudioManager) getSystemService(Service.AUDIO_SERVICE);
        if (mAudioManager.isWiredHeadsetOn()) {
            mCanEarback = true;
        }

        mReceiver = new HeadSetReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_HEADSET_PLUG);
        intentFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
        registerReceiver(mReceiver, intentFilter);

        mController = new Controller(this, this, null, null, null, null);

        mRLyric = findViewById(R.id.rlyric);
        mRLyric.init();
        mRLyric.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mPause) {
                    mController.pause();
                } else {
                    mController.resume();
                }
                mPause = !mPause;
            }
        });
        mCb = findViewById(R.id.cb);
        mCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mController.setOrg(isChecked);
            }
        });
        mCb2 = findViewById(R.id.cb2);
        mCb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                boolean succ = mController.setEarback(isChecked);
                if (!succ && isChecked) {
                    mCb2.setChecked(false);
                }
            }
        });
        mTv = findViewById(R.id.key_tv);

        mController.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mController.destroy();
        unregisterReceiver(mReceiver);
    }

    @Override
    public void onPlayerTime(long ms) {
        mRLyric.setMs(ms);
    }

    @Override
    public void onRecodTime(long ms) {
    }

    @Override
    public void currentVolume(double volume) {

    }


    public void jump(View v) {
        Intent i = new Intent(this, PlaybackActivity.class);
        i.putExtra("pitch", mController.getPitch());
        startActivity(i);
        finish();
    }

    public void up(View v) {
        mPitch++;
        if (mPitch > 10) {
            mPitch = 10;
        }
        if (mPitch != mController.getPitch()) {
            mController.setPitch(mPitch);
            mTv.setText("升降调：" + mPitch);
        }
    }

    public void dn(View v) {
        mPitch--;
        if (mPitch < -10) {
            mPitch = -10;
        }
        if (mPitch != mController.getPitch()) {
            mController.setPitch(mPitch);
            mTv.setText("升降调：" + mPitch);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (!mCanEarback) {
            return super.onKeyDown(keyCode, event);
        }
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                mAudioManager.adjustStreamVolume(
                        AudioManager.STREAM_VOICE_CALL,
                        AudioManager.ADJUST_RAISE,
                        AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
                return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                mAudioManager.adjustStreamVolume(
                        AudioManager.STREAM_VOICE_CALL,
                        AudioManager.ADJUST_LOWER,
                        AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
                return true;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    public class HeadSetReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if ("android.intent.action.HEADSET_PLUG".equals(action)) {
                if (mIsFirst) {
                    mIsFirst = false;
                    return;
                }
                if (intent.hasExtra("state")) {
                    mPause = true;
                    mController.pause();
                    if (intent.getIntExtra("state", 0) == 0) {
                        mCb2.setChecked(false);
                        mCanEarback = false;
                        mController.setCanEarback(false);
                    } else if (intent.getIntExtra("state", 0) == 1) {
                        mCanEarback = true;
                        mController.setCanEarback(true);
                    }
                }
            }
        }
    }
}
